﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNotebook.Inventory
{
    public class BrandManager:ConnectionManager
    {
        #region Adding/Updating
        public SqlException AddBrand(string _ItemGroup, string _BrandName)
        {
            SqlCommand oCommand = new SqlCommand();
            try
            {
                oCommand.Connection = this.Connection;
                oCommand.CommandText = "INSERT INTO M_BRND (ItemGroup, BrandName) VALUES (@ItemGroup, @BrandName)";
                oCommand.Parameters.Add(new SqlParameter("@ItemGroup", _ItemGroup));
                oCommand.Parameters.Add(new SqlParameter("@BrandName", _BrandName));

                oCommand.ExecuteNonQuery();

                return null;
            }
            catch (SqlException ex)
            {
                return ex;
            }
            finally
            {
                oCommand.Dispose();
            }
        }

        public SqlException UpdateBrand(string _BrandID, string _ItemGroup, string _BrandName)
        {
            SqlCommand oCommand = new SqlCommand();
            try
            {
                oCommand.Connection = this.Connection;
                oCommand.CommandText = "UPDATE M_BRND SET ItemGroup=@ItemGroup, BrandName=@BrandName WHERE BrandId=@BrandId";
                oCommand.Parameters.Add(new SqlParameter("@ItemGroup", _ItemGroup));
                oCommand.Parameters.Add(new SqlParameter("@BrandName", _BrandName));
                oCommand.Parameters.Add(new SqlParameter("@BrandId", _BrandID));

                oCommand.ExecuteNonQuery();
                return null;
            }
            catch (SqlException ex)
            {
                return ex;
            }
            finally
            {
                oCommand.Dispose();
            }
        }
        #endregion

        #region Getting Data
        public DataTable GetAllBrands()
        {
            DataSet ds = new DataSet();

            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM M_BRND";
            
            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            return ds.Tables[0];
        }
        public DataTable GetBrands(string _ItemGroup)
        {
            DataSet ds = new DataSet();

            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM M_BRND WHERE ItemGroup=@ItemGroup";
            oCommand.Parameters.Add(new SqlParameter("@ItemGroup", _ItemGroup));

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            return ds.Tables[0];
        }

        public DataTable GetBrandByID(string sBrandID)
        {
            DataSet ds = new DataSet();

            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM M_BRND WHERE BrandId=@BrandId";
            oCommand.Parameters.Add(new SqlParameter("@BrandId", sBrandID));

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            return ds.Tables[0];
        }

        public DataTable GetBrandByName(string sBrandName)
        {
            DataSet ds = new DataSet();
            sBrandName = '%' + sBrandName + '%';
            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM M_BRND WHERE BrandName LIKE @BrandName";
            oCommand.Parameters.Add(new SqlParameter("@BrandName", sBrandName));

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            return ds.Tables[0];
        }
        #endregion

        #region Others
        public bool IsBrandExists(string _BrandName)
        {
            DataSet ds = new DataSet();

            SqlCommand oCommand = new SqlCommand();
            SqlDataAdapter oAdapter = new SqlDataAdapter();

            oCommand.Connection = this.Connection;
            oCommand.CommandText = "SELECT * FROM M_BRND WHERE BrandName=@BrandName";
            oCommand.Parameters.Add(new SqlParameter("@BrandName", _BrandName));

            oAdapter.SelectCommand = oCommand;
            oAdapter.Fill(ds);

            int iCount = ds.Tables[0].Rows.Count;

            if (iCount >= 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion
    }
}
